package com.ruoyi.filter;



import com.ruoyi.project.battle.battle.domain.BattleUser;
import com.ruoyi.project.battle.battle.service.IBattleUserService;
import org.apache.shiro.web.filter.AccessControlFilter;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.List;

@Component
public class WebFileterLimit extends AccessControlFilter {

    IBattleUserService battleUserService;

    //获取applicationContext
   /* public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }


    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }
*/


    @Override
    protected boolean isAccessAllowed(ServletRequest servletRequest, ServletResponse servletResponse, Object o) throws Exception {

        HttpServletRequest  request=   (HttpServletRequest)servletRequest;
        HttpServletResponse  response=   (HttpServletResponse)servletResponse;
        HttpSession session = request.getSession();
        Object userId = session.getAttribute("userId");
        String requestURI = request.getRequestURI();
        long userIdL = Long.parseLong(userId.toString());

        if(userIdL==-1L){
            return true;
        }

        List<Integer> menuIds = battleUserService.selectLimitsByroleId(userIdL);
        if(requestURI.contains("/battle/battle") || requestURI.contains("/system/role") ) {
            response.reset();
            response.setContentType("text/html;charset=UTF-8");
            response.setHeader("Cache-Control", "no-cache");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
                writer.write("无权限，无法访问\n".toString());
                writer.flush();
                writer.close();
            return  false;
        }
        //前端子系统
        if(requestURI.contains("/monitor/online")){

            if(menuIds!=null && menuIds.contains(1)){
                return  true;
            }else {
                response.reset();
                response.setContentType("text/html;charset=UTF-8");
                response.setHeader("Cache-Control", "no-cache");
                response.setCharacterEncoding("UTF-8");
                PrintWriter writer = response.getWriter();
                writer.write("无权限，无法访问\n".toString());
                writer.flush();
                writer.close();
                return  false;
            }

        }

        if(requestURI.contains("/monitor/job")){

            if(menuIds!=null && menuIds.contains(2)){
                return  true;
            }else {
                response.reset();
                response.setContentType("text/html;charset=UTF-8");
                response.setHeader("Cache-Control", "no-cache");
                response.setCharacterEncoding("UTF-8");
                PrintWriter writer = response.getWriter();
                writer.write("无权限，无法访问\n".toString());
                writer.flush();
                writer.close();
                return  false;
            }

        }
        if(requestURI.contains("/monitor/data")){

            if(menuIds!=null && menuIds.contains(3)){
                return  true;
            }else {
                response.reset();
                response.setContentType("text/html;charset=UTF-8");
                response.setHeader("Cache-Control", "no-cache");
                response.setCharacterEncoding("UTF-8");
                PrintWriter writer = response.getWriter();
                writer.write("无权限，无法访问\n".toString());
                writer.flush();
                writer.close();
                return  false;
            }

        }
        if(requestURI.contains("/monitor/server")){

            if(menuIds!=null && menuIds.contains(4)){
                return  true;
            }else {
                response.reset();
                response.setContentType("text/html;charset=UTF-8");
                response.setHeader("Cache-Control", "no-cache");
                response.setCharacterEncoding("UTF-8");
                PrintWriter writer = response.getWriter();
                writer.write("无权限，无法访问\n".toString());
                writer.flush();
                writer.close();
                return  false;
            }

        }
        if(requestURI.contains("/monitor/cache")){

            if(menuIds!=null && menuIds.contains(5)){
                return  true;
            }else {
                response.reset();
                response.setContentType("text/html;charset=UTF-8");
                response.setHeader("Cache-Control", "no-cache");
                response.setCharacterEncoding("UTF-8");
                PrintWriter writer = response.getWriter();
                writer.write("无权限，无法访问\n".toString());
                writer.flush();
                writer.close();
                return  false;
            }

        }



        return true;
    }

    @Override
    protected boolean onAccessDenied(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception {
        HttpServletRequest  request=   (HttpServletRequest)servletRequest;
        HttpServletResponse  response=   (HttpServletResponse)servletResponse;
        HttpSession session = request.getSession();
        Object userId = session.getAttribute("userId");
        String requestURI = request.getRequestURI();
        long userIdL = Long.parseLong(userId.toString());
        if(userIdL==-1L){
            return true;
        }
        if(requestURI.contains("/battle/battle")) {
            response.setContentType("text/html;charset=UTF-8");
            response.setHeader("Cache-Control", "no-cache");
            response.setCharacterEncoding("UTF-8");


            response.getWriter().write("no permission\n".toString());
           return  false;
        }
        return true;
    }

    public void setBattleUserService(IBattleUserService battleUserService) {
        this.battleUserService = battleUserService;
    }
}
